<form class="layui-form seller-form" action="">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">部/院：</label>
            <div class="layui-input-inline" id="dep_box">
                <select name="dep_id" lay-filter="dep_id" id="dep_id">
                    <option value=''>请选择</option>
                    {volist name="depList" id="vo"}
                    <option value="{$vo.bmdm}">{$vo.bmmc}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">专业：</label>
            <div class="layui-input-inline" id="specialty_box">
                <select name="specialty_id" lay-filter="specialty_id" id="specialty_id" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">班级：</label>
            <div class="layui-input-inline" id="class_box">
                <select name="class_id" lay-filter="class_id" id="class_id" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">方向：</label>
            <div class="layui-input-inline" id="dir_box">
                <select name="dir" lay-filter="dir" id="dir" lay-search>
                    <option value=""></option>
                    <option value="1">就业</option>
                    <option value="2">升学</option>
                    <option value="3">创业</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">辅导员：</label>
            <div class="layui-input-inline">
                <input type="text" id="teacherName" name="teacherName" lay-verify="title" placeholder="请输入辅导员姓名" autocomplete="off" class="layui-input">
            </div>
        </div> 
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i
                    class="iconfont icon-chaxun"></i>查询</button>   
            <button type="button" class="layui-btn layui-btn-sm replace" lay-filter="replace"><i
                    class="iconfont icon-chaxun"></i>替换</button>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="classTeacherTable" lay-filter="classTeacherTable"></table>
</div>

<div id="replaceDetail" style="display: none;">
    <form class="layui-form replace-form"  action="" >    
        <div class="layui-form-item" style="margin: 20px;">
            <div class="layui-inline">
                <label class="layui-form-label"><i class="required-color">*</i> 辅导员：</label>    
                <div class="layui-input-inline" id="teacher_box">
                    <select class="batch_replace_teacher_id" name="teacher_id" lay-filter="teacher_id" id="teacher_id" lay-search>
                    </select>
                </div>
            </div>
        </div>
    </form>
</div>

<script>
    layui.use(['table', 'form', 'layer', 'laydate'], function () {
        var layer = layui.layer, table = layui.table, form = layui.form, date = layui.laydate;
        var con_dep_id = '';

        //执行渲染
        table.render({
            elem: '#classTeacherTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-195',
            cellMinWidth: '80',
            page: 'true',
            limit: '20',
            id: 'classTeacherTable',
            url: "{:url('BookCommon/returnNull')}",
            cols: [[ //标题栏
                { type: 'checkbox' },    
                { field: 'bmmc', sort: true, title: '部/院', width: 200, align: 'center' },
                { field: 'zymc', sort: true, title: '专业', width: 200, align: 'center' },
                { field: 'bjmc', sort: true, title: '班级', width: 200, align: 'center' },
                { field: 'dirName', sort: true, title: '方向', width: 80, align: 'center' },
                { field: 'truename', sort: true, title: '辅导员', width: 100, align: 'center' },
                { field: 'mobile', sort: true, title: '辅导员手机', width: 160, align: 'center' },
            ]], //设置表头
            where: { "dir": $("#dir").val() },
            done: function(res, curr, count){
            }
            //,…… //更多参数参考右侧目录：基本参数选项
        });

        // 查询
        form.on('submit(search)', function (data) {            
            con_dep_id = data.field.dep_id;
            if(!con_dep_id){
                layer.msg('部/院不能为空！', {time: 1300});
                return false;
            }

            table.reload('classTeacherTable', {
                where: data.field,
                page: {curr: 1},
                url: "{:url('student/batchReplaceIndex')}",
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
 
        
        // 联动专业
        layui.use('form', function () {
            form.on('select(dep_id)', function (data) {
                getSpecialty(data);
            });
            
            form.on('select(specialty_id)', function (data) {
                getClass(data);
            });
            
            // 联动参数
            form.on('select(type_id)', function (data) {
                getParams(data);
            });
            
            // 
            // form.on('select(teacher_id)', function () {
            //     getTeacherByDept();
            // });
        });
          
        // 得到专业
        function getSpecialty(data) {
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getSpecicalties')}",
                data: 'bmdm=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#specialty_id").empty();//清空下拉框的值
                        $('#specialty_id').append(new Option('', ''));// 下拉菜单里添加元素
                        
                        $("#class_id").empty();//清空下拉框的值
                        $('#class_id').append(new Option('', ''));// 下拉菜单里添加元素
                        $.each(e.data.data, function (index, item) {
                            $('#specialty_id').append(new Option(item.zymc, item.zydm));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };
        
        // 得到班级
        function getClass(data) { 
            var dept = $("#dep_id").val();
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getCWClasses')}",
                data: {
                        "bmdm": dept,
                        "zydm": data.value
                    },
                success: function(e) {
                    if(e.status) {
                        $("#class_id").empty();//清空下拉框的值
                        $('#class_id').append(new Option('', ''));// 下拉菜单里添加元素
                        $.each(e.data, function (index, item) {
                            $('#class_id').append(new Option(item.bjmc, item.bjdm));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };

        //替换
        $(document).on('click','.replace',function(){
            var checkStatus = table.checkStatus('classTeacherTable');
            data = checkStatus.data;
            if(data.length==0){
                layer.msg('请先选择一行数据！', { time: 1300 });
                return false;
            }

            let classes = ""; 
            $.each(data, function(index, item){ 
                classes += item.bmmc + ";" + item.zymc + ";" + item.bjmc + ";" + item.dirName + ";" + item.truename + ";" + item.mobile + ",";
            })
            classes = classes.substring(0, classes.length - 1);

            layer.open({
                type: 1,
                title:'替换',
                area: ['380px', '300px'], //宽高
                btn:['确定','取消'],
                content: $("#replaceDetail").html(),
                success:function() {
                    // 得到辅导员
                    $.ajax({
                        type: 'get',
                        url: "{:url('BookCommon/getAllTeachers')}",
                        data: {
                            // "bmdm": con_dep_id,
                        },
                        success: function(e) {
                            if(e.status) {
                                var aDiv=document.getElementsByClassName("batch_replace_teacher_id"); 
                                for (var i = 0;i<aDiv.length;i++) {
                                    $(aDiv[i]).empty();//清空下拉框的值
                                    $.each(e.data, function (index, item) {
                                        $(aDiv[i]).append(new Option(item.truename + ' ' + item.mobile, item.id));// 下拉菜单里添加元素
                                    });
                                }
                                layui.form.render("select");//重新渲染 固定写法
                            } else {
                                layer.msg(e.msg);
                            }
                        }
                    });
                },
                yes:function () {
                    if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                        $(".layui-layer-btn0").addClass("layui-btn-disabled");

                        var formData = $(".replace-form:last").serializeArray(); 
            
                        if($.trim(formData[0]['value']) == '') {
                            layer.msg('请选择辅导员！', { time: 1300 });
                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                            return false;
                        }
                        layer.msg($("#teacher_id").val(), { time: 1300 });
                        JsPost("{:url('Student/batchReplaceTeacher')}", {dep_id:con_dep_id, classes:classes,new_teacher_id:formData[0]['value'], remark:formData[0]['value']},function(res){
                                layer.msg(res.msg, {time:1500}, function(){
                                    if(res.status){
                                        layer.closeAll();
                                        //刷新当前页面
                                        table.reload('classTeacherTable', {
                                            where: data.field,
                                            page: {curr: 1},
                                            url: "{:url('student/batchReplaceIndex')}",
                                        });
                                    } else { 
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    }
                                });
                            }
                        );
                    }
                },btn2:function () {
                    layer.closeAll();
                }, end: function(){
                }
            });
            
        });

    });
</script>